package com.oba.base.service;

import javax.faces.context.FacesContext;

import com.icesoft.faces.async.render.RenderManager;
import com.icesoft.faces.async.render.Renderable;
import com.icesoft.faces.context.DisposableBean;
import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
import com.icesoft.faces.webapp.xmlhttp.RenderingException;
import com.oba.base.service.WebBean;

public class AbstractBean extends WebBean implements Renderable, DisposableBean {
	private RenderManager renderManager;
	private PersistentFacesState persistentFacesState;
	private String sessionId;

	public AbstractBean() {
		persistentFacesState = PersistentFacesState.getInstance();
		sessionId = FacesContext.getCurrentInstance().getExternalContext()
				.getSession(false).toString();

	}

	public void setRenderManager(RenderManager renderManager) {
		this.renderManager = renderManager;
		renderManager.getOnDemandRenderer(sessionId).add(this);
	}

	public PersistentFacesState getState() {
		return persistentFacesState;
	}

	public void renderingException(RenderingException renderingException) {
		renderingException.printStackTrace();
		renderManager.getOnDemandRenderer(sessionId).remove(this);
		renderManager.getOnDemandRenderer(sessionId).dispose();
	}

	public void dispose() throws Exception {
		renderManager.getOnDemandRenderer(sessionId).remove(this);
		renderManager.getOnDemandRenderer(sessionId).dispose();
	}

	protected void render() {
		renderManager.getOnDemandRenderer(sessionId).requestRender();
	}

}
